package com.maven.ioc.controller;


import com.maven.ioc.model.UserInfo;
import com.maven.ioc.rep.UserRepository;
import com.maven.ioc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/DI")
public class DIController {


/*
    @Qualifier("u1")
    @Autowired*/

    @Resource(name = "u1")
    private UserInfo userInfo;

    @RequestMapping("/u1")
    public void u1(HttpServletResponse response) throws IOException {
        response.getWriter().write(userInfo.toString());
    }




    // 从容器中取 数据
    // 方法一： 通过 @Autowired
    /*@Autowired
    private UserController userController;

    @RequestMapping("/u1")
    public void u1(){
        userController.u1();
    }*/

    // 方法二： 通过构造方法 来获取
    // 缺点:  不能 同时有多个 构造函数
    //  如果有多个构造方法来 获取 ,可以通过 @Autowired 来指定 构造方法
 /*   private UserService userService;
    private UserController userController;

    public DIController(UserService userService) {
        this.userService = userService;
   }
    @Autowired
    public DIController(UserController userController, UserService userService) {
        this.userController = userController;
        this.userService = userService;
    }

    @RequestMapping("/u2")
    public void u2(){
        userService.u1();
        userController.u1();
    }*/


    // 方法三: 通过 setter 来获取
    // 同样的 ,它 必须 配合 @Autowired 使用才行
   /* private UserController userController;

    @Autowired
    public void setUserController(UserController userController) {
        this.userController = userController;
    }

    @RequestMapping("/u3")
    public void u3() {
        userController.u1();
    }*/
}
